###   Implementation Constraints on natural climate solutions
###   Author: Kroeger, Timm; Erbaugh, James; Luo, Zhixian; Hilary Brumberg; Waverly Eichhorst; Margaret Hegwood; Anna LoPresti; Priya Shyamsundar; Peter W. Ellis; Lauren E. Oakes; Dow Martin; Pedro H. S. Brancalion; Mieke Bourne; Arundhati Jagadish; Kemen G. Austin; Andrew Kinzer; Marcos Sanjuán; Lisa McCullough; Marta Echavarria,

library(tidyverse)
library(dplyr)
library(ggplot2)
library(ggalluvial)

nbase<-read.csv("NBaseData_v1.csv")

nbase <- nbase %>%
  mutate(Constraint_category = ifelse(Constraint_category == "Government and Organizations", "Government and \n Organizations", Constraint_category)) %>%
  mutate(Constraint_category = ifelse(Constraint_category == "Negative side effects of NCS", "Negative side\n effects of NCS", Constraint_category)) %>%
  mutate(Constraint_category = ifelse(Constraint_category == "Social and Behavioral", "Social and \n Behavioral", Constraint_category)) %>%
  mutate(Subregion = ifelse(Subregion == "Australia and New Zealand", "Australia and\n New Zealand", Subregion)) %>%
  mutate(Subregion = ifelse(Subregion == "South-Eastern Asia", "South-Eastern\n Asia", Subregion)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "avoided coastal wetland conversion", "avoided\n coastal wetland\n conversion", NCS_Pathway)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "avoided forest conversion", "avoided forest \n conversion", NCS_Pathway)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "avoided grassland conversion", "avoided grassland \n conversion", NCS_Pathway)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "avoided peatland conversion", "avoided peatland \n conversion", NCS_Pathway)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "climate-smart forestry", "climate-smart\n forestry", NCS_Pathway)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "coastal wetland restoration", "coastal wetland\n restoration", NCS_Pathway)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "grassland restoration", "grassland \n restoration", NCS_Pathway)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "peatland restoration", "peatland \n restoration", NCS_Pathway)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "reduced woodfuel harvest in forests", "reduced woodfuel \n harvest in forest", NCS_Pathway)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "regenerative agriculture (other than agroforestry)", "regenerative\n agriculture\n (other than\n agroforestry)", NCS_Pathway)) %>%
  mutate(NCS_Pathway = ifelse(NCS_Pathway == "savanna management", "savanna \n management", NCS_Pathway))

nbase <- nbase %>%
  distinct(Study, NCS_Pathway, Subregion, Constraint_long, Constraint_category)

df_grouped <- nbase %>%
  group_by(Constraint_category, NCS_Pathway) %>%
  summarise(Freq = n(), .groups = "drop")  %>%
  mutate(Constraint_category = as.factor(Constraint_category),
         NCS_Pathway = factor(NCS_Pathway, levels = 
                                c("agroforestry",
                                  "avoided\n coastal wetland\n conversion",
                                  "avoided forest \n conversion",
                                  "avoided grassland \n conversion",
                                  "avoided peatland \n conversion",
                                  "climate-smart\n forestry",
                                  "coastal wetland\n restoration",
                                  "grassland \n restoration",
                                  "peatland \n restoration",
                                  "reforestation",
                                  "reduced woodfuel \n harvest in forest",
                                  "regenerative\n agriculture\n (other than\n agroforestry)",
                                  "savanna \n management")),
         Freq = as.numeric(Freq))

category_colors <- c("Finance" = "#77aadd", 
                     "Knowledge" = "#99ddff",
                     "Material Inputs" = "#44bb99",
                     "Social and \n Behavioral" = "#bbcc33",
                     "Government and \n Organizations" = "#aaaa00",
                     "Markets" = "#eedd88",
                     "Negative side\n effects of NCS" = "#ee8866",
                     "Rules and Laws" = "#ffaabb")

plot1 <- ggplot(df_grouped, aes(axis1 = Constraint_category, axis2 = Subregion, y = Freq)) +
  geom_alluvium(aes(fill = Constraint_category), width = 0.02, alpha = 0.8) +  
  geom_stratum(width = 0.02) +
  geom_text(stat = "stratum", aes(label = Constraint_category), size = 5 / .pt, nudge_x = -0.09, family = "Helvetica") +  # Move left axis labels to the left
  geom_text(stat = "stratum", aes(label = Subregion), size = 5 / .pt, nudge_x = 0.09, family = "Helvetica") +  # Move right axis labels to the right
  scale_fill_manual(values = category_colors, na.value = "grey") +
  theme_minimal() +
  theme(
    legend.position = "none",
    panel.grid = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title.y = element_text(face = "bold")  
  ) +
  labs(
    x = NULL,
    y = NULL,
    fill = "Constraint Category"
  )

ggsave("Supplementary Fig S7.jpg", plot = plot1, width = 180, height = 300, units = "mm", dpi = 300)

df_grouped <- nbase %>%
  group_by(Constraint_category, Subregion) %>%
  summarise(Freq = n(), .groups = "drop")  %>%
  mutate(Constraint_category = as.factor(Constraint_category),
         Subregion = factor(Subregion, levels = 
         c("Australia and\n New Zealand",
           "Eastern Asia",
           "Western Asia",
           "Southern Asia",
           "Central Asia",
           "South-Eastern\n Asia",
           "Northern Europe",
           "Western Europe",
           "Southern Europe",
           "Eastern Europe",
           "Western Africa",
           "Southern Africa",
           "Eastern Africa",
           "Northern Africa",
           "Middle Africa",
           "Melanisia",
           "Caribbean",
           "Northern America",
           "Central America",
           "South America")),
         Freq = as.numeric(Freq))


plot2 <- ggplot(df_grouped, aes(axis1 = Constraint_category, axis2 = NCS_Pathway, y = Freq)) +
  geom_alluvium(aes(fill = Constraint_category), width = 0.02, alpha = 0.8) +  
  geom_stratum(width = 0.02) +
  geom_text(stat = "stratum", aes(label = Constraint_category), size = 5 / .pt, nudge_x = -0.09, family = "Helvetica") +  # Move left axis labels to the left
  geom_text(stat = "stratum", aes(label = NCS_Pathway), size = 5 / .pt, nudge_x = 0.09, family = "Helvetica") +
  scale_fill_manual(values = category_colors, na.value = "grey") +
  theme_minimal() +
  theme(
    legend.position = "none",
    panel.grid = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title.y = element_text(face = "bold")  
  ) +
  labs(
    x = NULL,
    y = NULL,
    fill = "Constraint Category"
  )

ggsave("Supplementary Fig S8.pdf", plot = plot2, width = 180, height = 300, units = "mm", dpi = 300)

